const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 星星的数据拆分,尽量将方法写在utils文件夹下
function convertToStarsArray(stars) {
  // num代表拆分的数字
  var num =stars.substring(0,1);
  // 定义一个空数组存储星星的数据
  // 1：热度-》黑色星星
  // 0：空心星星
  var StartArr = [];
  for (var i=0;i<5;i++){
    if (i<num) {
      StartArr.push(1);
    }else{
      StartArr.push(0);
    }
  }
  return StartArr;
}

// 公共的网络请求
function http(url,callback){
  wx.request({
    url: url,
    method:"GET",
    header:{
      'content-type':'application/xml'
    },
    success:function(res){
          callback(res.data);
    }
  })
  }

  //时间戳转换成日期时间
function js_date_time(unixtime) {
  var dateTime = new Date(parseInt(unixtime) * 1000)
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString());  //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute+second;
  return timeSpanStr;
}

module.exports = {
  formatTime: formatTime,
  convertToStarsArray:convertToStarsArray,
  http:http,
  js_date_time:js_date_time
}
